package com.pimee.web.sys;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.pimee.common.util.shiro.ShiroUtils;
import com.pimee.model.SysRole;
import com.pimee.service.admin.ISysRoleService;
import com.pimee.web.AbstractController;

import io.swagger.annotations.ApiOperation;

@RequestMapping("admin/sys/role")
@Controller
public class SysRoleController extends AbstractController {
	private String prefix = "admin/sys/role";
	@Autowired
	private ISysRoleService sysRoleService;

	@ApiOperation("角色列表查询")
	@RequiresPermissions("system:role:list")
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public Object pageRole(@RequestParam Map<String, Object> params, ModelMap modelMap) {
		PageInfo<SysRole> pageRole = sysRoleService.pageRole(params);
		return setSuccessModelMap(modelMap, pageRole);
	}

	@ApiOperation("进入新增角色")
	@GetMapping("/toAdd")
	public String add() {
		return prefix + "/add";
	}

	@ApiOperation("保存角色新增信息")
	@RequiresPermissions("system:role:add")
	@PostMapping("/add")
	@ResponseBody
	public Object addSave(@Validated SysRole sysRole, ModelMap modelMap) {
		String loginName = ShiroUtils.getLoginName();
		sysRole.setCreateBy(loginName);
		sysRole.setUpdateBy(loginName);
		sysRoleService.insertRole(sysRole);
		return this.setSuccessModelMap(modelMap);
	}
	
	@ApiOperation("进入修改角色页面")
	@GetMapping("/toEdit/{roleId}")
	public String edit(@PathVariable("roleId") Long roleId, ModelMap modelMap) {
		modelMap.put("role", sysRoleService.selectByKey(roleId));
		return prefix + "/edit";
	}

	@ApiOperation("保存角色修改信息")
	@RequiresPermissions("system:role:edit")
	@PostMapping("/edit")
	@ResponseBody
	public Object editSave(@Validated SysRole sysRole, ModelMap modelMap) {
		sysRole.setUpdateBy(ShiroUtils.getLoginName());
		sysRoleService.updateRole(sysRole);
		return this.setSuccessModelMap(modelMap);
	}

	@ApiOperation("角色状态修改")
	@RequiresPermissions("system:role:edit")
	@PostMapping("/changeStatus")
	@ResponseBody
	public Object changeStatus(SysRole sysRole, ModelMap modelMap) {
		sysRoleService.updateRole(sysRole);
		return setSuccessModelMap(modelMap);
	}

	@ApiOperation("删除菜单")
	@RequiresPermissions("system:role:remove")
	@PostMapping("/remove/{roleId}")
	@ResponseBody
	public Object remove(@PathVariable("roleId") Long roleId, ModelMap modelMap) {
		sysRoleService.deleteRole(roleId);
		return setSuccessModelMap(modelMap);
	}

	@ApiOperation("进入角色授权页面")
	@GetMapping("/toAuthRoleMenu/{roleId}")
	public String toAuthRoleMenu(@PathVariable("roleId") Long roleId, ModelMap modelMap) {
		modelMap.put("role", sysRoleService.selectByKey(roleId));
		return prefix + "/authRoleMenu";
	}

	@ApiOperation("角色授权更新菜单")
	@RequiresPermissions("system:role:edit")
	@PostMapping("/updateMenu")
	@ResponseBody
	public Object updateMenu(Long roleId, String menuIds, ModelMap modelMap) {
		List<String> menuIdList = Arrays.asList(menuIds.split(","));
		sysRoleService.updateRoleMenu(roleId, menuIdList);
		return setSuccessModelMap(modelMap);
	}

}
